/**
 * Decider class implementation
 *
*/

#include "Decider.h"

int Decider::ChooseNewTask()
{
	DEBUG_PRINT("Decider choosing new task\n");
	BaseBehavior* behavior = NULL;
	bool found = false;
	int curPos = control->GetCurrentBehaviorPos();
	while(!found)
	{
		if(curPos == (control->GetSubBehaviors()->size() - 1))
		{
				found = true;
				behavior = NULL;
				break;
		}
        
		curPos++;
        
		behavior = control->GetSubBehaviors()->at(curPos);
		if(behavior->CheckConditions())
		{
				found = true;
		}
	}
    
	return curPos;
}

void Decider::BehaviorFailed() 
{
	control->SetCurrent(ChooseNewTask());
	if(control->GetCurrentBehavior() == NULL)
	{
			control->FinishWithFailure();
	}               
}